package org.yagnus.stats.samplers.discrete;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author hc.busy
 * 
 *         Some static utility methods used by the samplers.
 */
public class Utils {

	/**
	 * Makes the input doubles into probability boundaries
	 * 
	 * @param in
	 * @param smoother
	 *            the smoothing factor
	 * @return bunch of doubles that are non negative and adds to one.
	 */
	public static List<Double> makeProbability(List<Double> in, double smoother) {

		if (in.size() == 0)
			return in;
		if (smoother < 0)
			throw new IllegalArgumentException(
					"The smoothing parameter must be non-negative");

		double sum = 0d;
		for (int i = 0; i < in.size(); ++i) {
			if (in.get(i) < 0)
				throw new IllegalArgumentException(
						"The weight array must be non-negative");
			sum += in.get(i);
		}

		sum += in.size() * smoother;

		if (sum <= 0)
			throw new IllegalArgumentException(
					"The input array cannot be made into probabilities without smotoher, please specify a bigger smoother");

		ArrayList<Double> ret = new ArrayList<Double>(in.size());
		for (int i = 0; i < in.size(); ++i)
			ret.add((in.get(i) + smoother) / sum);

		return ret;
	}

}
